package com.kavian.blog;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.kavian.blog.collect.site.CollectCsdn;
import com.kavian.blog.dao.BlogDao;
import com.kavian.blog.domain.WPPost;

/**
 * 运行
 * @author kavian
 *
 */
public class TestRun{
	public static void main(String[] args) {
		HashMap<Integer, String> urlMap = new HashMap<Integer, String>();
		urlMap.put(3, "http://blog.csdn.net/mobile/index.html");//移动开发
		urlMap.put(4, "http://blog.csdn.net/web/index.html");//Web前端
		urlMap.put(5, "http://blog.csdn.net/enterprise/index.html");//架构设计
		urlMap.put(6, "http://blog.csdn.net/code/index.html");//编程语言
		
		CollectCsdn csdn = new CollectCsdn();
		BlogDao dao = new BlogDao();
		WPPost wp = new WPPost();
		WPPost temp = new WPPost();
		int i = 0;
		
		for(Entry<Integer, String> entry : urlMap.entrySet()){
			List<String> pageUrl = csdn.searchPageUrl(entry.getValue());
			if(null == pageUrl){
				continue;
			}
			int j = 0;
			for (String pUrl : pageUrl) {
				if(j>= 10){
					continue;
				}
				List<String> detailUrl = csdn.searchDetailedUrl(pUrl);
				if(null == detailUrl){
					continue;
				}
				for (String dUrl : detailUrl) {
					if(j>= 10){
						continue;
					}
					wp = csdn.findDetailed(dUrl);
					if(null == wp){
						continue;
					}
					temp = dao.findWPPostByTitle(wp.getTitle());
					if(null == temp){
						int maxId = dao.findMaxId();
						if(maxId == 0){
							continue;
						}
						wp.setId(maxId+1);
						dao.saveWPPost(wp);
						dao.savePostMeta(wp);
						dao.saveTermRelationships(wp.getId(),entry.getKey());
						System.out.println(dUrl);
						i++;
						j++;
					}else{
						continue;
					}
				}
			}
		}
		System.out.println(i);
	}
}